<?php

class ts_db{

    public $db_name;
    public $connected;

    public function __construct($database_name){
        $this->db_name = $database_name;
        $this->connected = false;
    }

    /**
     * @param string $host
     * @param string $username
     * @param string $password
     * 
     * @return boolean
     */

    public function connect($host , $username , $password){
        $connected = mysql_connect($host , $username , $password);

        if($connected){
            $connected = mysql_select_db($this->db_name);
            if($connected){
                $this->connected = true;
            }else{
                $this->connected = false;
            }
        }else{
            $this->connected = false;
        }

        return $this->connected;
    }

    /**
     * @param string $query_string
     * 
     * @return resource | boolean
     */

    public function query($query_string){
        $query = mysql_query($query_string) or die(mysql_error());
        return $query;
    }

    /**
     * @param string $table_name
     * @param array $fields
     *
     * @return boolean
     *
     * Usage:
     * ------
     * insert('table_name' , array(
     *                          'id' => 5 ,
     *                          'name' => 'tariq'
     *                          ));
     */

    public function insert($table_name , $fields){
        if($this->table_exists($table_name) && !empty($fields)){
            $a = array();
            $b = array();

            foreach($fields as $field => $value){
                $a[] = $field;
                $b[] = is_string($value) ? "'" . $value . "'" : $value;
            }

            $q = 'INSERT INTO ' . $table_name . ' (' . implode(',' , $a) . ') VALUES(' . implode(',' , $b) . ')';
            $q = $this->query($q);

            if($q){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * @param string $table_name
     * @param array $fields
     * @param array $where
     *
     * @return boolean
     *
     * Usage:
     * ------
     * update('table_name' , array(
     *                          'name' => 'tariq'
     *                          ) ,
     *                       array(
     *                          'id' => 5
     *                          ));
     */
    
    public function update($table_name , $fields , $where = null){
        if($this->table_exists($table_name) && !empty($fields)){
            $a = array();

            foreach($fields as $field => $value){
                $a[] = $field . ' = ' . (is_string($value) ? "'" . $value . "'" : $value);
            }

            $q = 'UPDATE ' . $table_name . ' SET ' . implode(',' , $a);

            if($where != null){
                $b = array();

                foreach($where as $field => $value){
                    $b[] = $field . ' = ' . (is_string($value) ? "'" . $value . "'" : $value);
                }

                $q .= ' WHERE ' . implode(' AND ' , $b);
            }

            $q = $this->query($q);

            if($q){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * @param string $table_name
     * @param array $where
     *
     * @return boolean
     *
     * Usage:
     * ------
     * delete('table_name' , array(
     *                          'id' => 5
     *                          ));
     */

    public function delete($table_name , $where = null){
        if($this->table_exists($table_name)){
            $q = 'DELETE FROM ' . $table_name;

            if($where != null){
                $a = array();

                foreach($where as $field => $value){
                    $a[] = $field . ' = ' . (is_string($value) ? "'" . $value . "'" : $value);
                }

                $q .= ' WHERE ' . implode(' AND ' , $a);
            }

            $q = $this->query($q);

            if($q){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * @param string $table_name
     * @param array | string $fields
     * @param array $where
     * @param array | string $order_by
     * @param string $order
     * @param array $like
     *
     * @return resource | boolean
     *
     * Usage:
     * ------
     * select('table_name' , 'name,age' , array(
     *                                       'id' => 5
     *                                          )
     *                                  , array(
     *                                       'id' , 'age'
     *                                          )
     *                                  , 'ASC'
     *                                  , array(
     *                                       'name' , 'tariq'
     *                                          ));
     */

    public function select($table_name , $fields = '*' , $where = null , $order_by = null , $order = 'ASC' , $like = null){
        if($this->table_exists($table_name)){
            if(is_array($fields)){
                $fields = implode(',' , $fields);
            }

            $q = 'SELECT ' . $fields . ' FROM ' . $table_name;

            if($where != null){
                $a = array();

                foreach($where as $field => $value){
                    $a[] = $field . ' = ' . (is_string($value) ? "'" . $value . "'" : $value);
                }

                $q .= ' WHERE ' . implode(' AND ' , $a);
            }

            if($like != null){
                if($where == null){
                    $q .= ' WHERE ';
                }

                $b = array();

                foreach($like as $field => $value){
                    $b[] = $field . " LIKE '" . $value . "'";
                }

                $q .= implode(' AND ' , $b);
            }

            if($order_by != null && ($order == 'ASC' || $order == 'DESC')){
                $q .= ' ORDER BY ' . (is_array($order_by) ? implode(',' , $order_by) : $order_by) . ' ' . $order;
            }

            $q = $this->query($q);

            if($q){
                return $q;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * @param string $table_name
     * @return boolean
     */

    public function table_exists($table_name){
        $q = 'SHOW TABLES FROM ' . $this->db_name . " LIKE '" . $table_name . "'";
        $q = $this->query($q);

        if($q){
            if(mysql_num_rows($q) == 0){
                return false;
            }else{
                return true;
            }
        }else{
            return false;
        }
    }

    /**
     * @param string $table_name
     * @param array $fields
     *
     * @return boolean
     *
     * Usage:
     * ------
     * create_table('users_table' , array(
     *                                    'id' => 'BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
     *                                    'name' => 'VARCHAR(30) ,
     *                                    ) ,
     *                              'CHARACTER SET utf8 COLLATE utf8_general_ci');
     */

    public function create_table($table_name , $fields , $options = null){
        if(!$this->table_exists($table_name) && !empty($fields)){
            $fields_str = array();
            foreach($fields as $col => $type){
                $fields_str[] = $col . ' ' . $type;
            }

            $q = 'CREATE TABLE ' . $table_name . ' (' . implode(',' , $fields_str) . ')' . ($options ? ' ' . $options : '');
            $q = $this->query($q);

            if($q){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * @param string $table_name
     * @return array | boolean
     */

    public function get_table_fields($table_name){
        if($this->table_exists($table_name)){
            $fields = array();
            $q = 'SHOW COLUMNS FROM ' . $table_name;
            $q = $this->query($q);

            while($field = mysql_fetch_assoc($q)){
                $fields[] = $field;
            }

            return $fields;
        }else{
            return false;
        }
    }

}

?>